package com.cn.wanxi.service;

import com.cn.wanxi.IOModel.CompanyIOModel;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.util.Path;
import com.cn.wanxi.util.ReadAndWriteIO;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class CompanyService {

    /**
     * 返回页面公司信息数据
     *
     * @return
     */
    public CompanyModel getCompanyModel() {
//        CompanyModel companyModel = new CompanyModel();
        /**
         * 1.要将数据写入记事本
         * 2.要将数据从记事本里面读取出来，读取出来的数据是字符串
         * 3.需要将字符串封装成为CompanyModel对象
         * 4。返回CompanyModel具体数据
         * 一个类里面只写关于当前类的方法或者属性，一个方法里面只做一件事情
         * 类变量 指静态变量
         */
        String name = Path.COMPANY_PATH.getPath();
        String split = Path.SPLIT.getPath();
        writeNote(name);
        String result = new ReadAndWriteIO().readNote(name);
        CompanyModel model = setCompanyModel(result, split);
        return model;
    }

    /**
     * 封装公司信息对象
     *
     * @return
     */
    private CompanyModel setCompanyModel(String result, String split) {
        String[] companyModels = result.split(split);
//        for (String s : companyModels) {
//
//        }
        CompanyModel companyModel = new CompanyModel();
        companyModel.setLogo(companyModels[0]);
        companyModel.setPhone(companyModels[1]);
        companyModel.setAd(companyModels[2]);
        companyModel.setNet(companyModels[3]);
        companyModel.setEmail(companyModels[4]);
        companyModel.setIntroduce(companyModels[5]);
        return companyModel;
    }


    /**
     * 写入记事本
     */
    private static void writeNote(String name) {
//            将数据写入记事本
        CompanyIOModel companyIOModel = new CompanyIOModel();
        List<String> list = new ArrayList<>();
        list.add(companyIOModel.getLogo());
        list.add(companyIOModel.getPhone());
        list.add(companyIOModel.getAd());
        list.add(companyIOModel.getNet());
        list.add(companyIOModel.getEmail());
        list.add(companyIOModel.getIntroduce());

    }

}
